package com.stone.framework.cases.mapper;

import java.util.Objects;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stone.framework.cases.entity.CaseInfo;
import com.stone.framework.cases.model.CaseConditionDTO;
import com.stone.framework.cases.model.CaseDTO;
import com.stone.framework.core.util.Beans;

@Mapper
public interface CaseInfoMapper extends BaseMapper<CaseInfo> {

    // @Select("select * from st_case_info sci inner join st_case_state scs on sci.org_code = scs.org_code and
    // sci.project_code = scs.project_code and sci.event_code = scs.event_code and sci.batch_code = scs.batch_code and
    // sci.case_nbr = scs.case_nbr ${ew.customSqlSegment}")
    IPage<CaseDTO> selectCurrCases(Page<CaseDTO> page, @Param(Constants.WRAPPER) Wrapper<CaseDTO> wrapper);

    IPage<CaseDTO> selectAllCases(Page<CaseDTO> page, @Param(Constants.WRAPPER) Wrapper<CaseDTO> wrapper);

    @SuppressWarnings("unused") // TODO
    default QueryWrapper<CaseDTO> condition(CaseConditionDTO condition) {
        QueryWrapper<CaseDTO> wrapper = new QueryWrapper<>();
        wrapper.eq(Objects.nonNull(condition.getOrgCode()), "sci.org_code", condition.getOrgCode());
        wrapper.eq(Objects.nonNull(condition.getProjectCode()), "sci.project_code", condition.getProjectCode());
        wrapper.eq(Objects.nonNull(condition.getEventCode()), "sci.event_code", condition.getEventCode());
        wrapper.eq(Objects.nonNull(condition.getBatchCode()), "sci.batch_code", condition.getBatchCode());

        wrapper.eq(Objects.nonNull(condition.getCaseNbr()), "sci.case_nbr", condition.getCaseNbr());
        wrapper.eq(Objects.nonNull(condition.getCustNbr()), "sci.cust_nbr", condition.getCustNbr());
        wrapper.like(Objects.nonNull(condition.getCustName()), "sci.cust_name", condition.getCustName());
        wrapper.eq(Objects.nonNull(condition.getTeamCode()), "sci.team_code", condition.getTeamCode());
        wrapper.ge(Objects.nonNull(condition.getStartDate()), "scs.start_date", condition.getStartDate());
        wrapper.le(Objects.nonNull(condition.getEndDate()), "scs.end_date", condition.getEndDate());
        wrapper.eq(Objects.nonNull(condition.getState()), "scs.state", condition.getState());
        if (false) { // TODO 当前登录人是员工角色
            wrapper.eq("sci.collector", "000001230"); // TODO session username
        } else {
            String deptId = ""; // TODO session deptId
            wrapper.likeRight(!"0305".equals(deptId), "sci.dept_id", deptId);
            wrapper.ne(Boolean.TRUE.equals(condition.isAllocated()), "sci.collector", "");
            wrapper.eq(Boolean.FALSE.equals(condition.isAllocated()), "sci.collector", "");
            wrapper.in(Beans.notBlank(condition.getCollector()), "sci.collector", condition.getCollector());
        }

        return wrapper;
    }

}
